//
//  NSDate+HHAddition.m
//  Addition
//
//  Created by 花花 on 2017/1/17.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "NSDate+HHAddition.h"

@implementation NSDate (HHAddition)
/**
 *  比较from和self的时间差值
 */

- (NSDateComponents *)deltaFrom:(NSDate *)from{
    
    //日历
    NSCalendar *calendar =[NSCalendar currentCalendar];
    
    //比较时间
    NSCalendarUnit unit = NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
    
    return [calendar components:unit fromDate:from toDate:self options:0];
    
    
}
/**
 *  是否为今年
 */
-(BOOL)isThisYear{
    
    //
    
    NSCalendar *calendar =[NSCalendar currentCalendar];
    
    NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
    
    NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
    
    return nowYear = selfYear;
    
    
}

/**
 *  是否为今天
 */
- (BOOL)isToday{
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *nowString = [formatter stringFromDate:[NSDate date]];
    
    NSString *selfString = [formatter stringFromDate:self];
    
    return [nowString isEqualToString:selfString];
    
    
}

/**
 *  是否为昨天
 */

- (BOOL)isYesterday{
    
    // 2014-12-31 23:59:59 -> 2014-12-31
    // 2015-01-01 00:00:01 -> 2015-01-01
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *nowDate = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
    
    NSDate *selfDate =[formatter dateFromString:[formatter stringFromDate: self]];
    
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components =[calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear  fromDate:selfDate toDate:nowDate options:0];
    
    return components.year ==0 &&components.month ==1 &&components.day == 1;
    
    
}

@end
